package com.learn.mqq.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

@Service
public class RedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public String getValueByKey(String key) {
        String encodedValue = stringRedisTemplate.opsForValue().get(key);
        if (encodedValue != null) {
            // 解码中文数据
            return new String(encodedValue.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
        }
        return null;
    }

    public void deleteByKey(String key) {
        stringRedisTemplate.delete(key);
    }
}
